package net.oaktoncc;

import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.PortInUseException;
import gnu.io.SerialPort;
import gnu.io.UnsupportedCommOperationException;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;

import net.oaktoncc.brultech_ecm1220.BrultechECM1220;

/**
 * TODO: Javadoc
 *
 * @author aeagle
 */
public class Main {
    public static void main(String[] args) throws PortInUseException, UnsupportedCommOperationException, IOException {
        System.out.println("Brultech ECM-1220 Data downloading...");

        SerialAuditDevice device = new BrultechECM1220();

        Enumeration thePorts = CommPortIdentifier.getPortIdentifiers();
        while (thePorts.hasMoreElements()) {
            CommPortIdentifier portIdentifier = (CommPortIdentifier) thePorts.nextElement();
            if (portIdentifier.getName().equals("/dev/cu.SLAB_USBtoUART")) {
                CommPort commPort = portIdentifier.open("AppName",2000);

                if ( !(commPort instanceof SerialPort)) {
                    throw new IllegalArgumentException("Port is not a serial port");
                }

                SerialPort serialPort = (SerialPort) commPort;
                device.setParams(serialPort);

                device.downloadData(serialPort, new PrintWriter(new File("output.txt")));

                commPort.close();
            }
        }
    }
}
